/*
 * Copyright 2012 Ufonzak.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.heathtow.web.pages;

import com.google.gson.Gson;
import cz.muni.fi.heathrowapi.AircraftService;
import cz.muni.fi.heathrowapi.DestinationService;
import cz.muni.fi.heathrowapi.FlightService;
import cz.muni.fi.heathrowapi.StewardService;
import cz.muni.fi.heathrowapi.dto.StewardDTO;
import java.util.ArrayList;
import java.util.HashSet;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author Ufonzak
 */
public class JSONRequests extends WebPage
{
    
    @SpringBean(name = "flightService")
    private FlightService mFlightService;
    @SpringBean(name = "destinationService")
    private DestinationService mDestService;
    @SpringBean(name = "stewardService")
    private StewardService mStewardService;
    @SpringBean(name = "aircraftService")
    private AircraftService mPlaneService;
    
    public JSONRequests(final PageParameters parameters)
    {
        String q = parameters.get("q").toOptionalString();
        String data = parameters.get("data").toOptionalString();
        int limit = parameters.get("limit").toInt(10);
        
        if (q.equals("aircraft"))
            getResponse().write(new Gson().toJson(
                    mPlaneService.findByCodeWild(data, limit)));
        else if (q.equals("destination"))
            getResponse().write(new Gson().toJson(
                    mDestService.findDestinationWild(data, limit)));
        else if (q.equals("steward"))
            getResponse().write(new Gson().toJson(
                    mStewardService.findByAnyName(data, limit)));
        else if (q.equals("flight_id"))
            getResponse().write(new Gson().toJson(
                    mFlightService.get(parameters.get("id").toOptionalLong())));
        else if (q.equals("steward_ids"))
        {
            HashSet<Long> stewIds = new HashSet<Long>();
            for (String id : parameters.get("data").toOptionalString().split(";"))
                stewIds.add(Long.valueOf(id));
            
            ArrayList<StewardDTO> stews = new ArrayList<StewardDTO>(stewIds.size());
            for (Long id : stewIds)
                stews.add(mStewardService.get(id));
            getResponse().write(new Gson().toJson(stews));
        }
    }
}
